from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
from Choose_coach_system.models import ChooseCoach
from evaluate_system.models import Comment


def comment(req):
    try:
        user = req.user
        title = req.POST.get('title')
        star = req.POST.get('stars')
        cid = req.POST.get('id')
        if not 0<int(req.POST.get('stars'))<6:
            raise Exception('0 < stars <6')
        train = ChooseCoach.objects.filter(id=int(cid)).first()

        com = Comment.objects.create(
            title=title,
            stars=star,
        )

        train.com = com

        train.save()

        return HttpResponse('<script>alert("Comment a successful"); window.history.go(-1)</script>')
    except Exception as e:
        return HttpResponse('<script>alert("Comment on failure; error:'+str(e)+'); window.history.go(-1)</script>')

